package com.ruoyi.biz.strategy.manager;

import com.ruoyi.biz.domain.BizGoodsCategory;
import com.ruoyi.biz.domain.dto.CaculateGoodsDto;
import com.ruoyi.biz.domain.vo.CaculateGoodsVo;
import com.ruoyi.biz.mapper.BizGoodsCategoryMapper;
import com.ruoyi.biz.strategy.IGoodsCategoryCaculateStrategy;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author tds
 * @version 1.0.0
 * @Company
 * @ClassName GoodsCategoryManager
 * @Description TODO（这里用一句话描述这个类的作用)
 * @Date 2022-11-10 22:58
 */
@Component
public class GoodsCategoryManager {

    /**
     * 商品类别策略map
     */
    private Map<String, IGoodsCategoryCaculateStrategy> goodsCategoryMap = new HashMap<>();

    @Resource
    private BizGoodsCategoryMapper goodsCategoryMapper;


    @Autowired
    public GoodsCategoryManager(List<IGoodsCategoryCaculateStrategy> strategyList){
        for (IGoodsCategoryCaculateStrategy goodsCategoryCaculateStrategy : strategyList) {
            goodsCategoryMap.put(goodsCategoryCaculateStrategy.getType(), goodsCategoryCaculateStrategy);
        }
    }

    /**
     * 计算商品类别价格
     *
     * @param caculateGoodsDto 商品类别计算信息
     * @return
     */
    public CaculateGoodsVo caculate(CaculateGoodsDto caculateGoodsDto) {
        // 查询商品类别信息
        BizGoodsCategory goodsCategory = goodsCategoryMapper.selectBizGoodsCategoryByGoodsId(caculateGoodsDto.getGoodsId());
        if (goodsCategory == null) {
            throw new ServiceException("商品类别信息不存在");
        }
        // 获取计算策略
        IGoodsCategoryCaculateStrategy caculateStrategy = goodsCategoryMap.get(goodsCategory.getCaculateType());
        if (caculateStrategy == null) {
            throw new ServiceException("未知的计算策略");
        }
        return caculateStrategy.caculate(caculateGoodsDto, goodsCategory);
    }
}
